import 'package:ceramics/extensions/ab_widget_extensions.dart';
import 'package:ceramics/model/ab_singleton.dart';
import 'package:ceramics/pages/main/ab_main_tab_page.dart';
import 'package:ceramics/pages/main/home/ab_home_detail_page.dart';
import 'package:ceramics/pages/main/video/ab_video_home_page.dart';
import 'package:ceramics/pages/main/video/ab_video_item.dart';
import 'package:ceramics/styles/ab_styles.dart';
import 'package:ceramics/util/ab_api.dart';
import 'package:ceramics/util/ab_loading.dart';
import 'package:ceramics/util/ab_log_util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

showMoreReport(String userId) {
  Get.bottomSheet(ABReportWidget(userId: userId));
}

class ABReportWidget extends StatelessWidget {
  final String userId;
  const ABReportWidget({super.key,required this.userId});

  @override
  Widget build(BuildContext context) {
    var divider = const Divider(color: ABStyles.lightBlack,height: 1);
    return Container(
      color: Colors.white,
      width: double.infinity,
      child: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildTextButton(ABSingleton.didFollow(userId) ? "Unfollow" : "Follow", _follow),
            divider,
            _buildTextButton("Block", _block),
            divider,
            _buildTextButton("Report", _report),
            Container(
              height: 10,
              color: const Color(0xFFF7F8FA),
            ),
            _buildTextButton("Cancel", ()=>Get.back())
          ],
        ),
      ),
    );
  }

  Widget _buildTextButton(String title,VoidCallback onTap) {
    return TextButton(onPressed: onTap,
        style: ElevatedButton.styleFrom(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
            backgroundColor: Colors.white,
            padding: EdgeInsets.zero),
        child: Text(title,style: ABStyles.tsNormalBlack15)).size(width: Get.width,height: 50);
  }

  _block() async {
    LogPvManger.stat(page: 'block');
    var res = await api.reportComplaintRecord(userId, 'Block');
    Get.back();
    await Future.delayed(const Duration(milliseconds: 1000));
    Get.back();
    await Future.delayed(const Duration(milliseconds: 500));
    toast('Block Success');
    var tabController = Get.find<ABMainTabController>();
    if (tabController.tabIndex.value == 1) {
      var vc = Get.find<ABVideoHomePageController>();
      vc.videoPlayerController?.play();
    }
  }

  _follow() async {
    if (ABSingleton.didFollow(userId)) {
      await api.unfollow(userId);
      ABSingleton.follows
          .removeWhere((element) => element.userId == userId);
    } else {
      await api.follow(userId);
      await ABSingleton.updateFollows();
    }
    try {
      Get.find<ABVideoHomePageController>().update();
    } catch (e) {
      print(e);
    }

    try {
      Get.find<ABHomeDetailController>().update();
    } catch (e) {
      print(e);
    }
    Get.back();
  }
  
  _report() async {
    Get.back();
    var options = ['Pornographic', 'False gender', 'Fraud', 'Political sensitive', 'Other'];
    Get.bottomSheet(Container(
      color: Colors.white,
      width: double.infinity,
      child: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            for (var option in options)
              Column(
                children: [
                  _buildTextButton(option, ()=>_reportOption(option)),
                  Divider(color: ABStyles.normalBlack.withOpacity(0.5),height: 1),
                ],
              ),
            Container(
              height: 10,
              color: const Color(0xFFF7F8FA),
            ),
            _buildTextButton('Cancel', ()=>Get.back())
          ],
        ),
      ),
    ));
    // var res = await api.reportComplaintRecord(userId, 'Report');
    // Get.back();
    // toast('Report Success');
  }

  _reportOption(String option) async {
    Get.back();
    LogPvManger.stat(page: 'report');
    await api.reportComplaintRecord(userId, 'Report', subCategory: option);
    toast('Report Success');
  }
}
